<script name="CallbackBind" lang="ts" setup>

  import useStore from '@/store';
import { QQUserInfo, QQUserInfoRes } from '@/types/data';
import { useCountDoen } from '@/utils/hooks';
import { mobileValid, codeValid } from '@/utils/validate';
import { useField, useForm } from 'vee-validate';
  import { ref } from 'vue';
  const qqUserInfo = ref<QQUserInfo>({} as QQUserInfo)
  // 获取当前QQ用户的信息
  if(QC.Login.check()) {
    QC.api('get_user_info').success((res: QQUserInfoRes) => {
      // console.log(res);
      qqUserInfo.value = res.data
    })
  }

  const {user} = useStore()
  // const mobile = ref('13012345714')
  // const code = ref('123456')
  const {counter, start} = useCountDoen()
  let openId = ''
  QC.Login.getMe(id => {
    openId = id
  })

  // 用户发送验证码
  const send = async () => {
    await user.sendQQBindMsg(mobile.value)
    start(10)
  }

  // 用户将当前登录的QQ和已有的账号进行绑定
  const bind = async () => {
    // 表单校验
    const {valid} = await validate()
    if(!valid) return

    await user.qqBindLogin(openId, mobile.value, code.value)
    alert('绑定成功')
  }

  // 表单校验
  const { validate } = useForm({
    validationSchema: {
      // 手机号
      mobile: mobileValid,
      // 手机号
      code: codeValid
    },
    initialValues: {
      mobile: '13012345714',
      code: '123456'
    }
  })
  const { value: mobile, errorMessage:mobileError, validate: mobileValidate } = useField<string>('mobile')
  const { value: code, errorMessage:codeError } = useField<string>('code')

</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img
        :src="qqUserInfo.figureurl_qq_2"
        alt=""
      />
      <p>Hi，{{qqUserInfo.nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model="mobile" class="input" type="text" placeholder="绑定的手机号" />
      </div>
      <!-- 校验失败的提示 -->
      <div class="error" v-if="mobileError">
        <i class="iconfont icon-warning" />{{mobileError}}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="code" class="input" type="text" placeholder="短信验证码" />
        <span class="code" @click="send">{{counter === 0 ? '发送验证码' : counter + '秒之后发送'}}</span>
      </div>
      <!-- 校验失败的提示 -->
      <div class="error" v-if="codeError">
        <i class="iconfont icon-warning" />{{codeError}}
      </div>
    </div>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>